# apps/roles/models.py
from django.db import models

from apps.tenants.models import Tenant


class Role(models.Model):
    """
    角色表
    """
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名")
    code = models.CharField(max_length=50, unique=True, verbose_name="角色码（如 admin）")
    description = models.TextField(blank=True, verbose_name="描述")
    menus = models.ManyToManyField('menus.Menu', blank=True, verbose_name="授权菜单")
    order = models.PositiveIntegerField(default=0, verbose_name="排序")
    is_active = models.BooleanField(default=True, verbose_name="启用")

    tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, null=True, db_index=True, verbose_name="租户")

    class Meta:
        db_table = 'qb_roles'
        verbose_name = '角色管理'
        verbose_name_plural = '角色管理'
        ordering = ['order']
        indexes = [
            models.Index(fields=['tenant'], name='idx_role_tenant'),
        ]

    def __str__(self):
        return self.name